home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / mosmllib / test / array.sml next >
Encoding:
Text File  |  1996-07-03  |  13.3 KB  |  339 lines  |  [TEXT/R*ch]

  1. (* test/array.sml -- some test cases for Array 
  2.    PS 1994-12-10, 1995-06-14 *)
  3.  
  4. use "auxil.sml";
  5.  
  6. local 
  7.     open Array 
  8.     infix 9 sub;
  9.     val a0 = array0;
  10. in
  11.  
  12. val a = fromList [0.1,1.1,2.1,3.1,4.1,5.1,6.1];
  13. val b = fromList [44.1,55.1,66.1];
  14. val c = fromList [0.1,1.1,2.1,3.1,4.1,5.1,6.1];
  15.  
  16. val test1 = check'(fn () => a<>c);
  17. val test2 = 
  18.     check'(fn () => 
  19.        array(0, 1.1) <> array0
  20.        andalso array(0,()) <> tabulate(0, fn _ => ())
  21.        andalso tabulate(0, fn _ => ()) <> fromList [] 
  22.        andalso fromList [] <> fromList [] 
  23.        andalso array(0, ()) <> array(0, ())
  24.        andalso tabulate(0, fn _ => ()) <> tabulate(0, fn _ => ()));
  25.  
  26. val d = tabulate(100, fn i => real (i mod 7) + 0.1);
  27.  
  28. val test3 = 
  29.     check'(fn () => d sub 27 = 6.1);
  30.  
  31. val test4a = (tabulate(maxLen+1, fn i => i) seq "WRONG")
  32.             handle Size => "OK" | _ => "WRONG";
  33.  
  34. val test4b = (tabulate(~1, fn i => i) seq "WRONG")
  35.             handle Size => "OK" | _ => "WRONG";
  36.  
  37. val test4c = 
  38.     check'(fn () => length (tabulate(0, fn i => i div 0)) = 0);
  39.  
  40. val test5a = 
  41.     check'(fn () => length (fromList []) = 0 andalso length a = 7);
  42.  
  43. val test5b = 
  44.     check'(fn () => length array0 = 0);
  45.  
  46. val test6a = (c sub ~1 seq "WRONG") handle Subscript => "OK" | _ => "WRONG";
  47. val test6b = (c sub 7  seq "WRONG") handle Subscript => "OK" | _ => "WRONG";
  48. val test6c = check'(fn () => c sub 0 = 0.1);
  49.  
  50. val e = array(203, 0.0);
  51. val _ = (copy{src=d, si=0, dst=e, di=0,        len=length d}; 
  52.      copy{src=b, si=0, dst=e, di=length d, len=length b};
  53.      copy{src=d, si=0, dst=e, di=length d + length b, len=length d});
  54.      
  55. fun a2v a = extract(a, 0, NONE);
  56. val ev = Vector.concat [a2v d, a2v b, a2v d]; (* length e = 203 *)
  57.  
  58. val test7 = check'(fn () => length e = 203);
  59.  
  60. val test8a = (update(e, ~1, 9.9) seq "WRONG")
  61.              handle Subscript => "OK" | _ => "WRONG";
  62. val test8b = (update(e, length e, 9.9) seq "WRONG")
  63.              handle Subscript => "OK" | _ => "WRONG";
  64.  
  65. val f = extract (e, 100, SOME 3);
  66.  
  67. val test9 = check'(fn () => f = a2v b);
  68.  
  69. val test9a = 
  70.     check'(fn () => ev = extract(e, 0, SOME (length e))
  71.        andalso ev = extract(e, 0, NONE));
  72. val test9b = 
  73.     check'(fn () => Vector.fromList [] = extract(e, 100, SOME 0));
  74. val test9c = (extract(e, ~1, SOME (length e))  seq "WRONG") 
  75.              handle Subscript => "OK" | _ => "WRONG"
  76. val test9d = (extract(e, length e+1, SOME 0) seq "WRONG") 
  77.              handle Subscript => "OK" | _ => "WRONG"
  78. val test9e = (extract(e, 0, SOME (length e+1)) seq "WRONG") 
  79.              handle Subscript => "OK" | _ => "WRONG"
  80. val test9f = (extract(e, 20, SOME ~1)        seq "WRONG") 
  81.              handle Subscript => "OK" | _ => "WRONG"
  82. val test9g = (extract(e, ~1, NONE)  seq "WRONG") 
  83.              handle Subscript => "OK" | _ => "WRONG"
  84. val test9h = (extract(e, length e+1, NONE) seq "WRONG") 
  85.              handle Subscript => "OK" | _ => "WRONG"
  86. val test9i = 
  87.     check'(fn () => a2v (fromList []) = extract(e, length e, SOME 0)
  88.         andalso a2v (fromList []) = extract(e, length e, NONE));
  89. val test9j =
  90.     check'(fn () => extract(e, 3, SOME(length e - 3)) = extract(e, 3, NONE));
  91.  
  92. val _ = copy{src=e, si=0, dst=e, di=0, len=length e};
  93. val g = array(203, 999999.9);
  94. val _ = copy{src=e, si=0, dst=g, di=0, len=length e};
  95.  
  96. val test10a = check'(fn () => ev = extract(e, 0, SOME (length e)) 
  97.                andalso ev = extract(e, 0, NONE));
  98. val test10b = check'(fn () => ev = extract(g, 0, SOME (length g))
  99.               andalso ev = extract(g, 0, NONE));
  100.  
  101. val _ = copy{src=g, si=203, dst=g, di=0, len=0};
  102. val test10c = check'(fn () => ev = extract(g, 0, SOME (length g)));
  103.  
  104. val _ = copy{src=g, si=0, dst=g, di=203, len=0};
  105. val test10d = check'(fn () => ev = extract(g, 0, SOME (length g)));
  106.  
  107. val _ = copy{src=g, si=0, dst=g, di=1, len=length g-1};
  108. val test10e = check'(fn () => a2v b = extract(g, 101, SOME 3));
  109.  
  110. val _ = copy{src=g, si=1, dst=g, di=0, len=length g-1};
  111. val test10f = check'(fn () => a2v b = extract(g, 100, SOME 3));
  112.  
  113. val _ = copy{src=g, si=202, dst=g, di=202, len=1};
  114. val test10g = 
  115.     check'(fn () => g sub 202 = real ((202-1-103) mod 7) + 0.1);
  116. val test10h = 
  117.     check'(fn () => (copy{src=array0, si=0, dst=array0, di=0, len=0}; 
  118.              array0 <> array(0, 99999.9)));
  119.  
  120. val test11a = (copy{src=g, si= ~1, dst=g, di=0, len=length g}; "WRONG") 
  121.               handle Subscript => "OK" | _ => "WRONG"
  122. val test11b = (copy{src=g, si=0, dst=g, di= ~1, len=length g}; "WRONG") 
  123.               handle Subscript => "OK" | _ => "WRONG"
  124. val test11c = (copy{src=g, si=1, dst=g, di=0, len=length g}; "WRONG") 
  125.               handle Subscript => "OK" | _ => "WRONG"
  126. val test11d = (copy{src=g, si=0, dst=g, di=1, len=length g}; "WRONG") 
  127.               handle Subscript => "OK" | _ => "WRONG"
  128. val test11e = (copy{src=g, si=0, dst=g, di=0, len=length g+1}; "WRONG") 
  129.               handle Subscript => "OK" | _ => "WRONG"
  130. val test11f = (copy{src=g, si=203, dst=g, di=0, len=1}; "WRONG") 
  131.               handle Subscript => "OK" | _ => "WRONG"
  132.  
  133.  
  134. local 
  135.     val v = ref 0
  136.     fun setv c = v := c;
  137.     fun addv c = v := c + !v;
  138.     fun setvi (i, c) = v := c + i;
  139.     fun addvi (i, c) = v := c + i + !v;
  140.     fun cons (x,r) = x ::  r
  141.     fun consi (i,x,r) = (i,x) ::  r
  142.     val inplist = [7,9,13];
  143.     val inp = fromList inplist
  144.     val pni = fromList (rev inplist)
  145.     fun copyinp a = 
  146.     copy{src=inp, si=0, dst=a, di=0, len=length inp}
  147. in 
  148.  
  149. val test12a =
  150.     check'(fn _ =>
  151.                foldl cons [1,2] array0 = [1,2]
  152.        andalso foldl cons [1,2] inp = [13,9,7,1,2]
  153.        andalso (foldl (fn (x, _) => setv x) () inp; !v = 13));
  154.  
  155. val test12b =
  156.     check'(fn _ =>
  157.                foldr cons [1,2] array0 = [1,2]
  158.        andalso foldr cons [1,2] inp = [7,9,13,1,2]
  159.        andalso (foldr (fn (x, _) => setv x) () inp; !v = 7));
  160.  
  161. (*
  162. val test12c =
  163.     check'(fn _ =>
  164.                find (fn _ => true) array0 = NONE
  165.        andalso find (fn _ => false) inp = NONE
  166.        andalso find (fn x => x=7) inp = SOME 7
  167.        andalso find (fn x => x=9) inp = SOME 9
  168.        andalso (setv 0; find (fn x => (addv x; x=9)) inp; !v = 7+9));
  169. *)
  170. val test12d = 
  171.     check'(fn _ =>
  172.            (setv 117; app setv array0; !v = 117)
  173.        andalso (setv 0; app addv inp; !v = 7+9+13)
  174.        andalso (app setv inp; !v = 13));
  175.  
  176. val test12e = 
  177.     let val a = array(length inp, inp sub 0)
  178.     in 
  179.     check'(fn _ =>
  180.            (modify (~ : int -> int) array0; true)
  181.        andalso (copyinp a; modify ~ a; foldr (op::) [] a = map ~ inplist)
  182.        andalso (setv 117; modify (fn x => (setv x; 37)) a; !v = ~13))
  183.     end
  184.  
  185. val test13a =
  186.     check'(fn _ =>
  187.                foldli consi [] (array0, 0, NONE) = []
  188.        andalso foldri consi [] (array0, 0, NONE) = []
  189.        andalso foldli consi [] (inp, 0, NONE) = [(2,13),(1,9),(0,7)]
  190.        andalso foldri consi [] (inp, 0, NONE) = [(0,7),(1,9),(2,13)])
  191. val test13b =
  192.     check'(fn _ =>
  193.                foldli consi [] (array0, 0, SOME 0) = []
  194.        andalso foldri consi [] (array0, 0, SOME 0) = []
  195.        andalso foldli consi [] (inp, 0, SOME 0) = []
  196.        andalso foldri consi [] (inp, 0, SOME 0) = []
  197.        andalso foldli consi [] (inp, 3, SOME 0) = []
  198.        andalso foldri consi [] (inp, 3, SOME 0) = []
  199.        andalso foldli consi [] (inp, 0, SOME 3) = [(2,13),(1,9),(0,7)]
  200.        andalso foldri consi [] (inp, 0, SOME 3) = [(0,7),(1,9),(2,13)]
  201.        andalso foldli consi [] (inp, 0, SOME 2) = [(1,9),(0,7)]
  202.        andalso foldri consi [] (inp, 0, SOME 2) = [(0,7),(1,9)]
  203.        andalso foldli consi [] (inp, 1, SOME 2) = [(2,13),(1,9)]
  204.        andalso foldri consi [] (inp, 1, SOME 2) = [(1,9),(2,13)]
  205.        andalso foldli consi [] (inp, 2, SOME 1) = [(2,13)]
  206.        andalso foldri consi [] (inp, 2, SOME 1) = [(2,13)]);
  207.  
  208. val test13c = (foldli consi [] (inp, ~1, NONE) seq "WRONG")
  209.            handle Subscript => "OK" | _ => "WRONG";
  210. val test13d = (foldli consi [] (inp, 4, NONE) seq "WRONG")
  211.            handle Subscript => "OK" | _ => "WRONG";
  212. val test13e = (foldli consi [] (inp, ~1, SOME 2) seq "WRONG")
  213.            handle Subscript => "OK" | _ => "WRONG";
  214. val test13f = (foldli consi [] (inp, 4, SOME 0) seq "WRONG")
  215.            handle Subscript => "OK" | _ => "WRONG";
  216. val test13g = (foldli consi [] (inp, 0, SOME 4) seq "WRONG")
  217.            handle Subscript => "OK" | _ => "WRONG";
  218. val test13h = (foldli consi [] (inp, 2, SOME ~1) seq "WRONG")
  219.            handle Subscript => "OK" | _ => "WRONG";
  220.  
  221. val test13i = (foldri consi [] (inp, ~1, NONE) seq "WRONG")
  222.            handle Subscript => "OK" | _ => "WRONG";
  223. val test13j = (foldri consi [] (inp, 4, NONE) seq "WRONG")
  224.            handle Subscript => "OK" | _ => "WRONG";
  225. val test13k = (foldri consi [] (inp, ~1, SOME 2) seq "WRONG")
  226.            handle Subscript => "OK" | _ => "WRONG";
  227. val test13l = (foldri consi [] (inp, 4, SOME 0) seq "WRONG")
  228.            handle Subscript => "OK" | _ => "WRONG";
  229. val test13m = (foldri consi [] (inp, 0, SOME 4) seq "WRONG")
  230.            handle Subscript => "OK" | _ => "WRONG";
  231. val test13n = (foldri consi [] (inp, 2, SOME ~1) seq "WRONG")
  232.            handle Subscript => "OK" | _ => "WRONG";
  233. (*
  234. val test14a =
  235.     check'(fn _ =>
  236.        findi (fn _ => true) (array0, 0, NONE) = NONE
  237.    andalso findi (fn _ => false) (inp, 0, NONE) = NONE
  238.    andalso findi (fn (i, x) => x=9 orelse 117 div (2-i) = 0) (inp, 0, NONE)
  239.        = SOME (1,9));
  240.  
  241. val test14b =
  242.     check'(fn _ =>
  243.        findi (fn _ => true) (array0, 0, SOME 0) = NONE
  244.    andalso findi (fn _ => false) (inp, 0, NONE) = NONE
  245.    andalso findi (fn (i, x) => x=9 orelse 117 div (2-i) = 0) (inp, 0, NONE)
  246.        = SOME (1,9));
  247.  
  248. val test14c = (findi (fn _ => true) (inp, ~1, NONE) seq "WRONG")
  249.            handle Subscript => "OK" | _ => "WRONG";
  250. val test14d = (findi (fn _ => true) (inp, 4, NONE) seq "WRONG")
  251.            handle Subscript => "OK" | _ => "WRONG";
  252. val test14e = (findi (fn _ => true) (inp, ~1, SOME 2) seq "WRONG")
  253.            handle Subscript => "OK" | _ => "WRONG";
  254. val test14f = (findi (fn _ => true) (inp, 4, SOME 0) seq "WRONG")
  255.            handle Subscript => "OK" | _ => "WRONG";
  256. val test14g = (findi (fn _ => true) (inp, 0, SOME 4) seq "WRONG")
  257.            handle Subscript => "OK" | _ => "WRONG";
  258. val test14h = (findi (fn _ => true) (inp, 2, SOME ~1) seq "WRONG")
  259.            handle Subscript => "OK" | _ => "WRONG";
  260. *)
  261. val test15a = 
  262.     check'(fn _ =>
  263.            (setvi (0,117); appi setvi (array0, 0, NONE); !v = 117)
  264.        andalso (setvi (0,0); appi addvi (inp, 0, NONE); !v = 0+7+1+9+2+13)
  265.        andalso (appi setvi (inp, 0, NONE); !v = 2+13));
  266. val test15b = 
  267.     check'(fn _ =>
  268.            (setvi (0,117); appi setvi (array0, 0, SOME 0); !v = 117)
  269.        andalso (setvi (0,0); appi addvi (inp, 0, SOME 0); !v = 0)
  270.        andalso (setvi (0,0); appi addvi (inp, 3, SOME 0); !v = 0)
  271.        andalso (setvi (0,0); appi addvi (inp, 0, SOME 2); !v = 0+7+1+9)
  272.        andalso (setvi (0,0); appi addvi (inp, 1, SOME 2); !v = 1+9+2+13)
  273.        andalso (setvi (0,0); appi addvi (inp, 0, SOME 3); !v = 0+7+1+9+2+13)
  274.        andalso (appi setvi (inp, 1, SOME 2); !v = 2+13)
  275.        andalso (appi setvi (inp, 0, SOME 2); !v = 1+9)
  276.        andalso (appi setvi (inp, 0, SOME 1); !v = 0+7)
  277.        andalso (appi setvi (inp, 0, SOME 3); !v = 2+13));
  278.  
  279. val test15c = (appi setvi (inp, ~1, NONE) seq "WRONG")
  280.            handle Subscript => "OK" | _ => "WRONG";
  281. val test15d = (appi setvi (inp, 4, NONE) seq "WRONG")
  282.            handle Subscript => "OK" | _ => "WRONG";
  283. val test15e = (appi setvi (inp, ~1, SOME 2) seq "WRONG")
  284.            handle Subscript => "OK" | _ => "WRONG";
  285. val test15f = (appi setvi (inp, 4, SOME 0) seq "WRONG")
  286.            handle Subscript => "OK" | _ => "WRONG";
  287. val test15g = (appi setvi (inp, 0, SOME 4) seq "WRONG")
  288.            handle Subscript => "OK" | _ => "WRONG";
  289. val test15h = (appi setvi (inp, 2, SOME ~1) seq "WRONG")
  290.            handle Subscript => "OK" | _ => "WRONG";
  291.  
  292. val test16a = 
  293.     let val a = array(length inp, inp sub 0)
  294.     in 
  295.     check'(fn _ =>
  296.            (modifyi (op +) (array0, 0, NONE); true)
  297.        andalso (modifyi (op +) (array0, 0, SOME 0); true)
  298.        andalso (copyinp a; modifyi (op -) (a, 0, SOME 0); 
  299.             foldr (op::) [] a = [7,9,13])
  300.        andalso (copyinp a; modifyi (op -) (a, 3, SOME 0); 
  301.             foldr (op::) [] a = [7,9,13])
  302.        andalso (copyinp a; modifyi (op -) (a, 0, NONE); 
  303.             foldr (op::) [] a = [~7,~8,~11])
  304.        andalso (copyinp a; modifyi (op -) (a, 0, SOME 3); 
  305.             foldr (op::) [] a = [~7,~8,~11])
  306.        andalso (copyinp a; modifyi (op -) (a, 0, SOME 2); 
  307.             foldr (op::) [] a = [~7,~8,13])
  308.        andalso (copyinp a; modifyi (op -) (a, 1, SOME 2); 
  309.             foldr (op::) [] a = [7,~8,~11])
  310.        andalso (copyinp a; setv 117; 
  311.             modifyi (fn x => (setvi x; 37)) (a, 0, NONE); !v = 2+13)
  312.        andalso (copyinp a; setv 117; 
  313.             modifyi (fn x => (setvi x; 37)) (a, 0, SOME 3); !v = 2+13)
  314.        andalso (copyinp a; setv 117; 
  315.             modifyi (fn x => (setvi x; 37)) (a, 1, SOME 2); !v = 2+13)
  316.        andalso (copyinp a; setv 117; 
  317.             modifyi (fn x => (setvi x; 37)) (a, 0, SOME 2); !v = 1+9)
  318.        andalso (copyinp a; setv 117; 
  319.             modifyi (fn x => (setvi x; 37)) (a, 0, SOME 0); !v = 117)
  320.        andalso (copyinp a; setv 117; 
  321.             modifyi (fn x => (setvi x; 37)) (a, 3, SOME 0); !v = 117))
  322.     end
  323.  
  324. val test16b = (modifyi (op+) (inp, ~1, NONE) seq "WRONG")
  325.            handle Subscript => "OK" | _ => "WRONG";
  326. val test16c = (modifyi (op+) (inp, 4, NONE) seq "WRONG")
  327.            handle Subscript => "OK" | _ => "WRONG";
  328. val test16d = (modifyi (op+) (inp, ~1, SOME 2) seq "WRONG")
  329.            handle Subscript => "OK" | _ => "WRONG";
  330. val test16e = (modifyi (op+) (inp, 4, SOME 0) seq "WRONG")
  331.            handle Subscript => "OK" | _ => "WRONG";
  332. val test16f = (modifyi (op+) (inp, 0, SOME 4) seq "WRONG")
  333.            handle Subscript => "OK" | _ => "WRONG";
  334. val test16g = (modifyi (op+) (inp, 2, SOME ~1) seq "WRONG")
  335.            handle Subscript => "OK" | _ => "WRONG";
  336. end
  337.  
  338. end
  339.